import { BasicColumn, FormSchema } from "/@/components/Table"
import { rules } from "/@/utils/helper/validator"
import { listAllPayWay } from "/@/api/pay"
import { render } from "/@/utils/common/renderUtils"
import { DescItem } from "/@/components/Description/index"

const isCustomPage = (type) => type === "2"

export const columns: BasicColumn[] = [
  {
    title: "接口名称",
    width: 100,
    dataIndex: "ifName",
  },
  {
    title: "接口代码",
    width: 100,
    dataIndex: "ifCode",
  },
  {
    title: "状态",
    width: 60,
    dataIndex: "status",
    customRender: ({ text }) => {
      let color = "green"
      if (text == "0") color = "red"
      if (text == "1") color = "green"

      return render.renderTag(render.renderDict(text, "data_use_status"), color)
    },
  },
  {
    title: "创建时间",
    width: 150,
    dataIndex: "createdAt",
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    field: "ifCode",
    label: "接口代码",
    component: "Input",
    // colProps: {span: 12},
  },
  {
    field: "ifName",
    label: "接口名称",
    component: "Input",
    // colProps: {span: 12},
  },
  {
    label: "状态",
    field: "status",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "data_use_status",
      placeholder: "请选择支付接口状态",
    },
    // colProps: {span: 6},
  },
]

export const formDetailSchema: DescItem[] = [
  {
    field: "ifCode",
    label: "接口代码",
    span: 2,
  },
  {
    field: "ifName",
    label: "接口名称",
    span: 2,
  },
  {
    field: "createdAt",
    span: 2,
    label: "创建时间",
  },
  {
    field: "isMchMode",
    label: "普通商户模式",
    span: 2,
    render: (curVal, data) => {
      let color = "green"
      if (curVal == "0") color = "#FFCC33"
      if (curVal == "1") color = "green"

      return render.renderTag(render.renderDict(curVal, "support_status"), color)
    },
  },
  {
    field: "isIsvMode",
    label: "服务商子商户模式",
    span: 2,
    render: (curVal, data) => {
      let color = "green"
      if (curVal == "0") color = "#FFCC33"
      if (curVal == "1") color = "green"

      return render.renderTag(render.renderDict(curVal, "support_status"), color)
    },
  },
  {
    field: "configPageType",
    label: "支付参数配置页面类型",
    span: 2,
    render: (curVal, data) => {
      let color = "green"
      if (curVal == "0") color = "#FFCC33"
      if (curVal == "1") color = "green"

      return render.renderTag(render.renderDict(curVal, "pay_config_page_type"), color)
    },
  },
  {
    field: "isvParams",
    label: "服务商接口配置",
    span: 4,
    labelMinWidth: 120,
    render: (curVal, data) => {
      if (!curVal) {
        return ""
      }
      return render.renderInputTextArea({
        value: curVal,
        autoSize: {
          minRows: 2,
          maxRows: 10,
        },
        readOnly: true,
      })
    },
  },
  {
    field: "isvsubMchParams",
    label: "特约商户接口配置",
    span: 4,
    labelMinWidth: 120,
    render: (curVal, data) => {
      if (!curVal) {
        return ""
      }
      return render.renderInputTextArea({
        value: curVal,
        autoSize: {
          minRows: 2,
          maxRows: 10,
        },
        readOnly: true,
      })
    },
  },
  {
    field: "normalMchParams",
    label: "普通商户渠道配置",
    span: 4,
    labelMinWidth: 120,
    render: (curVal, data) => {
      if (!curVal) {
        return ""
      }
      return render.renderInputTextArea({
        value: curVal,
        autoSize: {
          minRows: 2,
          maxRows: 10,
        },
        readOnly: true,
      })
    },
  },

  {
    field: "enableCheckBill",
    label: "是否开启对账",
    span: 2,
    render: (curVal, data) => {
      let color = "green"
      if (curVal) color = "green"
      else color = "red"

      return render.renderTag(curVal ? "开启" : "关闭", color)
    },
  },

  {
    field: "status",
    label: "状态",
    span: 2,
    render: (curVal, data) => {
      let color = "green"
      if (curVal == "0") color = "red"
      if (curVal == "1") color = "green"

      return render.renderTag(render.renderDict(curVal, "data_use_status"), color)
    },
  },

  {
    field: "wayCodes",
    label: "支持的支付方式",
    span: 4,
    render: (curVal, data) => {
      let color = "green"
      if (curVal == "0") color = "red"
      if (curVal == "1") color = "green"

      return render.renderTag(curVal, color)
    },
  },
  {
    field: "remark",
    label: "备注",
    span: 4,
  },
]

export const formSchema: FormSchema[] = [
  {
    label: "id",
    field: "id",
    component: "Input",
    show: false,
  },
  {
    field: "ifCode",
    label: "接口代码",
    required: true,
    component: "Input",
    colProps: { span: 12 },
    dynamicDisabled: ({ values }) => {
      return !!values.id
    },
  },
  {
    field: "ifName",
    label: "接口名称",
    required: true,
    component: "Input",
    colProps: { span: 12 },
  },
  {
    field: "isMchMode",
    label: "是否普通商户模式",
    required: true,
    component: "JDictSelectTag",
    componentProps: {
      dictType: "support_status",
      type: "radio",
    },

    colProps: { span: 12 },
  },
  {
    field: "isIsvMode",
    label: "是否支持服务商子商户模式",
    required: true,
    component: "JDictSelectTag",
    componentProps: {
      type: "radio",
      dictType: "support_status",
    },
    colProps: { span: 12 },
  },
  {
    field: "configPageType",
    label: "支付参数配置页面类型",
    required: true,
    labelWidth: 150,
    component: "JDictSelectTag",
    componentProps: {
      type: "radio",
      dictType: "pay_config_page_type",
      placeholder: "请选择页面类型",
    },
    colProps: { span: 12 },
  },
  {
    field: "normalParamsScope",
    label: "普通渠道参数作用域",
    required: true,
    labelWidth: 150,
    component: "JDictSelectTag",
    defaultValue: "channel",
    helpMessage: [
      "1. 为了支持类似支付宝这种一个应用需要单独配置参数而设计的",
      "2. 假如你的一个应用需要支持支付宝H5, 小程序等多端, 可以选择作用域是支付方式",
    ],
    componentProps: {
      type: "radio",
      dictType: "pay_normalParamsScope",
    },
    colProps: { span: 12 },
  },

  {
    field: "isvParams",
    labelWidth: 150,
    label: "服务商接口配置定义描述",
    slot: "isvParams",
    component: "InputTextArea",
    colProps: { span: 24 },
    ifShow: ({ values }) => {
      return values.isIsvMode ? values.isIsvMode == "1" : true && !isCustomPage(values.configPageType)
    },
  },
  {
    field: "isvsubMchParams",
    labelWidth: 160,
    label: "特约商户接口配置定义描述",
    component: "InputTextArea",
    slot: "isvsubMchParams",
    colProps: { span: 24 },
    ifShow: ({ values }) => {
      return (values.isIsvMode ? values.isIsvMode == "1" : true) && !isCustomPage(values.configPageType)
    },
  },
  {
    field: "normalMchParams",
    labelWidth: 160,
    label: "普通商户渠道配置定义描述",
    component: "InputTextArea",
    slot: "normalMchParams",
    colProps: { span: 24 },
    ifShow: ({ values }) => {
      return values.isMchMode ? values.isMchMode == "1" : true && !isCustomPage(values.configPageType)
    },
  },
  {
    field: "status",
    label: "状态",
    component: "JDictSelectTag",
    defaultValue: "1",
    required: true,
    colProps: { span: 12 },
    componentProps: {
      dictType: "data_use_status",
      type: "radio",
    },
  },
  {
    field: "enableCheckBill",
    label: "是否开启对账",
    component: "RadioGroup",
    required: true,
    colProps: { span: 12 },
    componentProps: {
      options: [
        {
          label: "是",
          value: true,
        },
        {
          label: "否",
          value: false,
        },
      ],
    },
  },

  {
    field: "remark",
    label: "备注",
    component: "InputTextArea",
  },
  {
    label: "支持的支付方式",
    field: "wayCodes",
    component: "ApiSelect",
    colProps: { span: 24 },
    componentProps: {
      mode: "multiple",
      api: listAllPayWay,
      labelField: "wayName",
      valueField: "wayCode",
    },
  },
]
